package cc.ufinity.networkdiagnose.widget;
 
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import cc.ufinity.networkdiagnose.app.GlobalApp;
import cc.ufinity.networkdiagnose.manager.FTPConfig;
import cc.ufinity.networkdiagnose.manager.RandomGUID;
import cc.ufinity.networkdiagnose.manager.Task;
import cc.ufinity.networkdiagnose.manager.Task.FTPTask;
import cc.ufinity.networkdiagnose.manager.Task.HttpTask;
import cc.ufinity.networkdiagnose.manager.Task.PingTask;
import cc.ufinity.networkdiagnose.manager.TelManager;
import cc.ufinity.networkdiagnose.manager.WlanManager;
import cc.ufinity.networkdiagnose.view.R;

public class SelectNetType_main extends GeneralTestWidget implements View.OnClickListener {
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Activity ctx;
    private Task task;
	private TelManager tm;
    private TaskDetailWidget tdw;
    private WlanManager wlanManager; 
    private Test_Task_Widget ttw2;
    
    private TestWidget_Wlan tw;
    private TestWidget_TD tw_TD;
    private TestWidget_GSM tw_GSM;
    
    private String networkType; 
    
    public SelectNetType_main(final Activity context) {
        super(context);
        inflate(context, R.layout.nettype_main, this);
        btn1 = (Button) this.findViewById(R.id.button1_main);
        btn2 = (Button) this.findViewById(R.id.button2_main);
        btn3 = (Button) this.findViewById(R.id.button3_main); 
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this); 
        this.ctx = context;
        
         ctx.getIntent().putExtra(TAG_REFER_TASK, task);
         ctx.getIntent().putExtra("from", "TestWidget");
   //      ctx.setContentView(new SelectNetType(ctx));

         
        /*task = (Task) ctx.getIntent().getSerializableExtra(TAG_REFER_TASK);
        if(task.scanResult != null && task.scanResult.SSID.equals(WlanManager.SSID_CMCC)){
        //	showCMCCAuthDialog(context);
         }*/
     	wlanManager = new WlanManager(ctx);
		tm = new TelManager(ctx);
    }
    
    private void initTasks() {
    	
    	String randomTaskId = (new RandomGUID()).valueAfterMD5;
        SharedPreferences settings;
        String urlString;
        HttpTask httpTask;
        List<HttpTask> httpTasks;
        FTPConfig ftpConfig;
        String ftpString;
        FTPTask ftpTask;
        List<FTPTask> ftpTasks;
        PingTask pingTask;
        List<PingTask> pingTasks;
        String number;
        
         task = new Task();
         task.id = randomTaskId;
         settings = ctx.getSharedPreferences(HttpConfigWidget.PREFS_HTTP_NAME, 0);
         urlString = settings.getString(HttpConfigWidget.PREFS_HTTP_KEY, "");
         urlString = (urlString ==null||urlString.equals(""))?"www.baidu.com":urlString;
         httpTask = task.newHttpTask(urlString);
         httpTasks = new ArrayList<Task.HttpTask>();
         httpTasks.add(httpTask);
         task.httpTasks = httpTasks; 

         ftpConfig = getFTPConfig(ctx);
         ftpString =ftpConfig==null?"": ftpConfig.address+","+ftpConfig.userName+","+ftpConfig.password;
         ftpTask = task.newFTPTask(ftpString);
         ftpTasks = new ArrayList<Task.FTPTask>();
         ftpTasks.add(ftpTask);
         task.ftpTasks = ftpTasks;   

         pingTask = task.newPingTask("www.baidu.com");
         pingTasks = new ArrayList<Task.PingTask>();
         pingTasks.add(pingTask);
         task.pingTasks = pingTasks; 
         settings = ctx.getSharedPreferences(PingConfigWidget.PREFS_PING_NAME, 0);
         number = settings.getString(PingConfigWidget.PREFS_PING_KEY, "4");
         number = (number==null || number.equals(""))?"4":number;
         task.testCount = Integer.parseInt(number);
         task.taskType = "网络测试";
         task.testType = 1;
         task.selNetType = ConnectivityManager.TYPE_WIFI;
         GlobalApp.taskWlan = task;
         
         task = new Task();
         task.id = randomTaskId;
         settings = ctx.getSharedPreferences(HttpConfigWidget.PREFS_HTTP_NAME, 0);
         urlString = settings.getString(HttpConfigWidget.PREFS_HTTP_KEY, "");
         urlString = (urlString ==null||urlString.equals(""))?"www.baidu.com":urlString;
         httpTask = task.newHttpTask(urlString);
         httpTasks = new ArrayList<Task.HttpTask>();
         httpTasks.add(httpTask);
         task.httpTasks = httpTasks; 

         ftpConfig = getFTPConfig(ctx);
         ftpString =ftpConfig==null?"": ftpConfig.address+","+ftpConfig.userName+","+ftpConfig.password;
         ftpTask = task.newFTPTask(ftpString);
         ftpTasks = new ArrayList<Task.FTPTask>();
         ftpTasks.add(ftpTask);
         task.ftpTasks = ftpTasks;   

         pingTask = task.newPingTask("www.baidu.com");
         pingTasks = new ArrayList<Task.PingTask>();
         pingTasks.add(pingTask);
         task.pingTasks = pingTasks; 
         settings = ctx.getSharedPreferences(PingConfigWidget.PREFS_PING_NAME, 0);
         number = settings.getString(PingConfigWidget.PREFS_PING_KEY, "4");
         number = (number==null || number.equals(""))?"4":number;
         task.testCount = Integer.parseInt(number);
         task.taskType = "网络测试";
         task.testType = 1;
         task.selNetType = ConnectivityManager.TYPE_MOBILE_MMS;
         GlobalApp.taskTD = task;
         
         
         task = new Task();
         task.id = randomTaskId;
         settings = ctx.getSharedPreferences(HttpConfigWidget.PREFS_HTTP_NAME, 0);
         urlString = settings.getString(HttpConfigWidget.PREFS_HTTP_KEY, "");
         urlString = (urlString ==null||urlString.equals(""))?"www.baidu.com":urlString;
         httpTask = task.newHttpTask(urlString);
         httpTasks = new ArrayList<Task.HttpTask>();
         httpTasks.add(httpTask);
         task.httpTasks = httpTasks; 

         ftpConfig = getFTPConfig(ctx);
         ftpString =ftpConfig==null?"": ftpConfig.address+","+ftpConfig.userName+","+ftpConfig.password;
         ftpTask = task.newFTPTask(ftpString);
         ftpTasks = new ArrayList<Task.FTPTask>();
         ftpTasks.add(ftpTask);
         task.ftpTasks = ftpTasks;   

         pingTask = task.newPingTask("www.baidu.com");
         pingTasks = new ArrayList<Task.PingTask>();
         pingTasks.add(pingTask);
         task.pingTasks = pingTasks; 
         settings = ctx.getSharedPreferences(PingConfigWidget.PREFS_PING_NAME, 0);
         number = settings.getString(PingConfigWidget.PREFS_PING_KEY, "4");
         number = (number==null || number.equals(""))?"4":number;
         task.testCount = Integer.parseInt(number);
         task.taskType = "网络测试";
         task.testType = 1;
         task.selNetType = ConnectivityManager.TYPE_MOBILE;
         GlobalApp.taskGSM = task;
         
         GlobalApp.setTestType(1);
    }

    private void connectWIFI() {
    	showMessageDialog(ctx, "正在连接wifi...");
    	new Thread(){

			@Override
			public void run() {
				while(true) {  
					if(wlanManager.checkNetworkType() == 1) {
						
						Message msg = handler.obtainMessage();
						msg.arg1 = 1;
						msg.sendToTarget();
		                
		                break;
					}
	    		}
			}
    			
    	}.start();
    }
    
    Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {

			if(msg.arg1 == 1) {
				tw = new TestWidget_Wlan(ctx);
                ctx.setContentView(tw);
                dismissMessageDialog();
			}
			super.handleMessage(msg);
		}
    	
    };
    
    @Override
    public void onClick(View v) {
    	GlobalApp.taskOtherList.clear();
    	GlobalApp.index = 0;
    	switch (v.getId()) {
        case R.id.button1_main:
        	initTasks();
        	GlobalApp.nowNetType = 1;
        	wlanManager.openWifi();
            wlanManager.setMobileDataEnabled(ctx,false); 
        	tw = new TestWidget_Wlan(ctx);
            ctx.setContentView(tw);
            break;
        case R.id.button2_main:
        	GlobalApp.nowNetType = 2;
        	networkType = tm.getNetworkType();
        	if(GlobalApp.DEBUG) {
        		if(tm.getNetworkType()!=null && 
        				(networkType.equals("EDGE") || networkType.equals("GPRS"))){
    				showAlertDialog(ctx,"当前网络类型为GSM模式，进行TD测试请先切换至TD网络类型.");
    			} else {
            		initTasks();
     	            wlanManager.closeWifi();
                    wlanManager.setMobileDataEnabled(ctx,true);
     	            tw_TD = new TestWidget_TD(ctx);
    	            ctx.setContentView(tw_TD);
    			}
        	} else {
            	System.out.println("-------- 手机网络类型： " + networkType);
            	if(networkType.equals("UNKNOW"))  {
            		//未知网络
            		showMessage(getContext(), "网络未知，请更正网络");
            	} else if(networkType.equals("HSDPA"))  {
            		//TD网络
            		initTasks();
            		wlanManager.closeWifi();
                    wlanManager.setMobileDataEnabled(ctx,true);
     	            tw_TD = new TestWidget_TD(ctx);
    	            ctx.setContentView(tw_TD);
            	} else if(networkType.equals("EDGE") || networkType.equals("GPRS"))  {
            		//GSM网络
            		showAlertDialog(ctx,"当前网络类型为GSM模式，进行TD测试请先切换至TD网络类型.");
            	} else {
            		//其他网络
            		showMessage(getContext(), "当前不在TD网络，无法测试");
            	}
        	} 
            break;
        case R.id.button3_main:
        	GlobalApp.nowNetType = 3;
        	networkType = tm.getNetworkType();
        	if(GlobalApp.DEBUG) {
            	System.out.println("-------- 手机网络类型： " + networkType);
                if (tm.getNetworkType()!=null && tm.getNetworkType().equals("HSDPA")) {
                	showAlertDialog(ctx,"当前网络类型为TD模式，进行GSM测试请先切换至GSM网络类型.");
   			 	}else {
	             	initTasks();
					wlanManager.closeWifi();
					wlanManager.setMobileDataEnabled(ctx,true);
					tw_GSM = new TestWidget_GSM(ctx);
					ctx.setContentView(tw_GSM);
	   			}
        	} else {
            	System.out.println("-------- 手机网络类型： " + networkType);
            	if(networkType.equals("UNKNOW"))  {
            		//未知网络
            		showMessage(getContext(), "网络未知，请更正网络");
            	} else if(networkType.equals("HSDPA"))  {
            		//TD网络
            		showAlertDialog(ctx,"当前网络类型为TD模式，进行GSM测试请先切换至GSM网络类型.");
            	} else if(networkType.equals("EDGE") || networkType.equals("GPRS"))  {
            		//GSM网络
                	initTasks();
            		wlanManager.closeWifi();
                    wlanManager.setMobileDataEnabled(ctx,true);
    	             tw_GSM = new TestWidget_GSM(ctx);
     	             ctx.setContentView(tw_GSM);
            	} else {
            		//其他网络
            		showMessage(getContext(), "当前不在GSM网络，无法测试");
            	}
        	}
            break;
        /*case R.id.btn_nettype_back_main: 
    		this.back(ctx);
            break;*/
        }
    	
    }
}
